<template>
  <div class="app-container">
    <!-- 筛选栏 -->
    <div class="filter-container">
      <!-- 输入框 -->
      <el-select
        v-model="filters.logLevel"
        class="filter-item"
        style="width: 200px"
        placeholder="日志级别"
        @keyup.enter.native="handleFilter"
      >
        <el-option :value="undefined">全部</el-option>
        <el-option
          v-for="item in logLevelOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-input
        v-model="filters.nodeId"
        placeholder="节点编码"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <!-- 按钮 -->
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh"
        @click="handleClean"
      >
        清理日志
      </el-button>
    </div>

    <!-- 数据Table -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <!-- 表格字段 -->
      <el-table-column label="服务编码" align="center" width="80">
        <template slot-scope="{ row }">
          <span>{{ row.nodeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日志级别" align="center" width="135" :formatter="logLevelFormatter" />
      <el-table-column label="文件名" align="center" width="275">
        <template slot-scope="{ row }">
          <span>{{ row.logFileName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.logMessage }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" width="155">
        <template slot-scope="{ row }">
          <span>{{ row.logTime }}</span>
        </template>
      </el-table-column>

      <!-- 操作按钮 -->
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <!-- 详情 -->
          <el-button size="mini" @click="handleInfo(row)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <!-- 编辑弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        :disabled="showInfo"
        style="width: 98%; margin-left: 10px"
      >
        <el-input
          v-model="temp.logMessage"
          :autosize="{ minRows: 2, maxRows: 15 }"
          type="textarea"
          placeholder="请输入描述"
        />
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// 引入API接口函数
import { pageList, clean } from '@/api/video/serviceNodeLog'
// 分页
import Pagination from '@/components/Pagination'

export default {
  name: 'ServiceNodeLog',
  components: { Pagination },
  data() {
    return {
      // 表格数据
      list: null,
      // 数据总数
      total: 0,
      // 表格加载中
      listLoading: false,
      listQuery: {
        // 当前页码
        pageNo: 1,
        // 数据条数
        pageSize: 20,
        // 参数
        params: undefined
      },
      filters: {},
      // 状态下拉框选项数据
      logLevelOptions: ['DEBUG', 'INFO', 'WARNING', 'ERROR'],
      // 编辑时的模板数据
      temp: {
        id: undefined,
        remark: '',
        status: '1'
      },
      // 编辑弹窗状态：create | update | info
      dialogStatus: '',
      // 显示编辑弹窗
      dialogFormVisible: false,
      // 编辑弹窗-标题
      textMap: {
        update: '编辑',
        create: '添加',
        info: '详情'
      },
      // 编辑弹窗-表单校验规则
      rules: {
        timestamp: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 展示详情
    showInfo() {
      return this.dialogStatus === 'info'
    }
  },
  created() {
    // 默认加载查询一次表格数据
    this.getList()
  },
  methods: {
    // 处理搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置模板数据
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    // 查看详情
    handleInfo(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'info'
      this.dialogFormVisible = true
    },
    // 获取表格数据
    getList() {
      this.listLoading = true
      this.listQuery.filters = this.filters
      pageList(this.listQuery)
        .then((response) => {
          this.list = response.data.list
          this.total = response.data.totalRow
          // 模拟请求时间
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
        .catch((_) => {
          this.listLoading = false
        })
    },
    handleClean() {
      clean().then((response) => {
        if (response.data) {
          this.$notify({
            title: '成功',
            message: '清理成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.findIndex(v => v.id === this.temp.id)
          this.list.splice(index, 1)
        } else {
          this.$notify({
            title: '失败',
            message: '清理失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    logLevelFormatter(row, column) {
      switch (row.logLevel) {
        case 'DEBUG':
          return <el-button type='info' plain>DEBUG</el-button>
        case 'INFO':
          return <el-button type='success' plain>INFO</el-button>
        case 'WARNING':
          return <el-button type='warning' plain>WARNING</el-button>
        case 'ERROR':
          return <el-button type='danger' plain>ERROR</el-button>
        default:
          return ''
      }
    }
  }
}
</script>

<style scoped lang="scss"></style>
